package HashMapSeries;

import java.util.HashMap;
import java.util.Map;

public class CanConstructSolution_383 {
    public boolean canConstruct(String ransomNote, String magazine) {
        // 设计一个hashMap key=字母 value=出现个数
        Map<Character, Integer> record = new HashMap<>();

        // 根据magazine建立哈希map
        for (int i = 0; i < magazine.length(); i++) {
            // 如果存在
            if (record.containsKey(magazine.charAt(i))) {
                record.put(magazine.charAt(i), record.get(magazine.charAt(i)) + 1);
            } else {
                record.put(magazine.charAt(i), 1);
            }
        }

        for (int i = 0; i < ransomNote.length(); i++) {
            if (record.containsKey(ransomNote.charAt(i))) {
                record.put(ransomNote.charAt(i), record.get(ransomNote.charAt(i)) - 1);
                if (record.get(ransomNote.charAt(i)) < 0) {
                    return false;
                }
            } else {
                return false;
            }

        }
        return true;
    }
}
